/*
 *  File: SafeColor216vlet.java 
 *
 *  @author jeffliu
 *  @date Jun 24, 2011
 *  @version 
 */
package com.yulookbook.web;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.yulookbook.image.ColorBucketsPixelDataObserver;
import com.yulookbook.image.ImageProcessor;
import com.yulookbook.util.Cache;
import com.yulookbook.web.output.BucketPoolOutputObserverFactory;
import com.yulookbook.web.output.ColorBucketPoolOutput;
import com.yulookbook.web.util.CacheFactory;

// TODO: Auto-generated Javadoc
/**
 * Servlet implementation class SafeColor216vlet.
 */
public class SafeColor216vlet extends HttpServlet {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public SafeColor216vlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * Do get.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		Cache cache = CacheFactory.createCache();
		
		BaseRequestParam params = new BaseRequestParam(request);
		
		String strurl = params.getParam("url");
		
		String format = params.getParam("format");
		
		
		//String where = "http://cdn3.lookbook.nu/files/looks/medium/2011/06/15/1339117_lookbook89_.jpg?1308146682";
		if(strurl!=null && strurl.trim().length()>0){
			if(cache.containKey(strurl+"-"+format)){
				String cachedData = (String)cache.get(strurl+"-"+format);
				response.getWriter().append(cachedData);
			}else{	
				URL url = new URL(strurl);
				BufferedImage image = ImageIO.read(url);
				
				ColorBucketsPixelDataObserver observer = new ColorBucketsPixelDataObserver();
				
				ImageProcessor proccessor = new ImageProcessor();
				proccessor.addObserver(observer);
				proccessor.proccess(image);
				
				ColorBucketPoolOutput output = new ColorBucketPoolOutput();
				StringBuffer sb = new StringBuffer();
				
				output.addObservers(BucketPoolOutputObserverFactory.getObservers(format, response, sb));
				
				output.output(observer.getPool());
				
				cache.put(strurl+"-"+format,sb.toString());
				
				image.flush();
				image = null;
			}
		}
	}

	/**
	 * Do post.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
